<template>
  <div>
    <!--1.视图-->
    <!--    搜索-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="批次号">
        <el-input
          v-model="queryParams.materialCode"
          placeholder="批次号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <!--    页面-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['car:type:add']"
        >新增</el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['system:role:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:role:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport">
          <!--          v-hasPermi="['system:post:export']"-->
          >导出</el-button>
      </el-col>
    </el-row>

    <template>
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        @selection-change="handleSelectionChange">
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          fixed
          prop="materialLotId"
          label="编号"
          width="150">
        </el-table-column>
        <el-table-column
          prop="materialCode"
          label="批次号"
          width="120">
        </el-table-column>
        <el-table-column
          prop="farmerName"
          label="农户名称"
          width="120">
        </el-table-column>
        <el-table-column
          prop="cropCode"
          label="农产品编码"
          width="120">
        </el-table-column>
        <el-table-column
          prop="eggsTime"
          label="创建时间"
          width="120">
        </el-table-column>
        <el-table-column
          prop="validity"
          label="有效期"
          width="120">
        </el-table-column>
        <el-table-column
          prop="address"
          label="地址"
          width="120">
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="100">
          <template slot-scope="scope">
            <el-button @click="handleUpdate(scope.row)" type="text" size="small">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>

    <!--    分页-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getMaterList"
    />

    <!--    添加/修改-->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="农户名称" prop="farmerId" >
          <el-select v-model="form.farmerId" placeholder="农户名称">
            <el-option
              v-for="item in farmers"
              :key="item.farmerId"
              :label="item.farmerName"
              :value="item.farmerId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="产蛋时间" prop="eggsTime">
          <el-input v-model="form.eggsTime" placeholder="请输入产蛋时间" type="date" />
        </el-form-item>
        <el-form-item label="有效期" prop="validity">
          <el-input v-model="form.validity" placeholder="请输入有效期" />
        </el-form-item>

        <el-form-item label="农产品编码" prop="cropCode">
          <el-select v-model="form.cropCode" placeholder="批次编号">
            <el-option
              v-for="c in cropList"
              v-bind:key="c.cropId"
              :label="c.cropCode"
              :value="c.cropCode">
            </el-option>
          </el-select>
        </el-form-item>

        <!--        三级联动-->
        <el-form-item label="请选择地区" prop="cityValue">
          <el-cascader
            v-model="cityValue"
            :options="options"
            :props="{
            value: 'id',
            label: 'name',
            children: 'children'
          }"
            @change="handleChange">
          </el-cascader>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open=false">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import {
  listCity , listMaterial , addMaterial , deleteMaterialById , updateMaterial , getById , listCrop
  } from "@/api/material/batch";
import {getFarmerList} from "@/api/product/products";

export default {
  name: "index",
  data() {
    return {
      //2属性定义
      // 表单校验
      rules: {
        farmerId: [
          { required: true, message: "农户名称不能为空", trigger: "change" }
        ],
        eggsTime: [
          { required: true, message: "产蛋时间不能为空", trigger: "change" }
        ],
        validity: [
          { required: true, message: "有效期不能为空", trigger: "blur" }
        ],
        cropCode: [
          { required: true, message: "批次编号不能为空", trigger: "change" }
        ]
      },
      tableData:[],
      farmers: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialCode: undefined
      },
      total:0,
      showSearch:true,
      open:false,   // 打开表单
      title:'',     //标题 添加/修改
      form: {},   //添加 / 修改
      ids:'',
      cityValue:[],
      options:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      cropList:[],
    }
  }, methods: {
    //3.方法
    /** 查询农户列表 */
    selectFarmerList(){
      getFarmerList().then(response => {
        this.farmers = response.data;
      });
    },
    handleChange(val){
      console.log(val);
      this.cityValue=val;
      this.form.xid=val[val.length-3];
      this.form.yid=val[val.length-2];
      this.form.zid=val[val.length-1];
    },
    //三级联动
    getcitylist(){
      listCity().then(res=>{
        this.options=res.data;
      })
    },
    getMaterList(){
      listMaterial(this.queryParams).then(res=>{
        this.tableData=res.rows;
        this.total=res.total;
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getMaterList();
    },
    handleAdd() {
      this.form={};
      this.selectFarmerList()
      this.title = "添加原材料批次号列表";
      this.open = true;
    },
    submitForm: function() {
      console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.title==="添加原材料批次号列表"){
            addMaterial(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getMaterList();
            });
          } else{
            updateMaterial(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getMaterList();
            });
          }
        }
      });
    },
    handleSelectionChange(val){     // 获取多选id 的值
      this.ids=val.map(res=>res.materialLotId).join(",");
      this.single = val.length!=1;
      this.multiple = !val.length
      console.log(this.ids);
    },
    handleUpdate(row) {
      console.log(row)
      this.form={};
      this.selectFarmerList();
      this.cityValue=[row.xid+'',row.yid+'',row.zid+''];
      this.form=row;
      this.open=true;
      this.title="修改原材料批次号列表";
    },
    handleDelete(){             //删除
      const ids =this.ids;
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function() {
        return deleteMaterialById(ids);
      }).then(() => {
        this.getMaterList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('crop/material/export', {
        ...this.queryParams
      }, `post_${new Date().getTime()}.xlsx`)
    },
    getcropList(){
      listCrop().then(res=>{
        this.cropList=res.data;
        console.log(this.cropList);
      })
    }

  }, created() {
    //4.钩子函数
    this.getMaterList();
    this.getcitylist();
    this.getcropList();
  }
}
</script>

<style scoped>

</style>
